package lan.battletank.element;

import lan.battletank.environment.EnvConst;
import lan.battletank.environment.Stage;
import lan.battletank.logic.ElementStatusLogic;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.util.FloatMath;

public class BaseBulletElement extends BaseElement {

    /** Bullet state. */
    protected final int STATE_INIT = 0;

    protected final int STATE_FLYING = 1;

    protected final int STATE_EXPLODING = 2;

    protected int damage = 35; // bullet damage

    protected final float POWER_FACTOR = 5.0f; // bullet power factor

    /** Bullet current tilt angle. */
    protected float bulletTiltAngle;

    /** Bullet initial speed. */
    protected float bulletSpeedX;

    protected float bulletSpeedY;

    protected int mState;

    /** Bullet image to use */
    protected BitmapDrawable mBulletImage;

    /** Explosion effect object. */
    protected ExplosionEntity mExplosion;

    /**
     * constructor
     */
    public BaseBulletElement() {
        reset();
    }

    public void draw(Canvas c) {
        if (mState == STATE_FLYING) {
            c.save();
            c.rotate((float) bulletTiltAngle, (float) centerX, (float) centerY);
            mBulletImage.setBounds((int) (centerX - halfWidth), (int) (centerY - halfHeight), (int) (centerX + halfWidth), (int) (centerY + halfHeight));
            mBulletImage.draw(c);
            c.restore();
        } else if (mState == STATE_EXPLODING) {
            mExplosion.draw(c);
        }
    }

    public void setFire(int gunX, int gunY) {
        centerX = gunX + width / 2;
        centerY = gunY;
    }

    public void reset() {
        centerX = 0;
        centerY = 0;
        mState = STATE_INIT;
        bulletSpeedX = 0;
        bulletSpeedY = 0;
        bulletTiltAngle = 0;
        bulletSpeedX = 0;
        bulletSpeedY = 0;
    }

    /**
     * Set the bullet initial motion parameters by firing angle and fire speed.
     * @param fireAngle : 0 - 90 (degree)
     * @param power : 0 - 100
     */
    public void setBulletmomentum(int fireAngle, int power) {
        bulletTiltAngle = fireAngle;
        bulletSpeedX = power * POWER_FACTOR * FloatMath.cos((float) Math.toRadians(fireAngle));
        bulletSpeedY = power * POWER_FACTOR * FloatMath.sin((float) Math.toRadians(fireAngle));
    }

    public boolean shouldExplode(Stage stage, BaseBattleUnitElement target, int canvasWidth, int canvasHeight) {
        if (!isOutOfBoundX(canvasWidth) && stage.bulletCollide(this)) {
            return true;
        }
        if (ElementStatusLogic.isColliding(this, target)) {
            target.setHit(true);
            return true;
        } else if (centerY > (canvasHeight - EnvConst.LAND_HEIGHT - halfHeight)) {
            return true;
        }
        return false;
    }

    private boolean isOutOfBoundX(int canvasWidth) {
        return (centerX + halfWidth < 0 || centerX - halfWidth > canvasWidth);
    }
}
